<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加操作员</title>
    <link rel="stylesheet" href="../../plugins/element-ui/index.css"/>
    <link rel="stylesheet" href="../../styles/common.css"/>
    <link rel="stylesheet" href="../../styles/page.css"/>
</head>
<body>
<div class="addBrand-container" id="member-add-app">
    <div class="container">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" :inline="false" label-width="180px" class="demo-ruleForm">
            <el-form-item label="用户名:" prop="username">
                <el-input v-model="ruleForm.username" placeholder="请输入用户名" maxlength="20"/>
            </el-form-item>
            <el-form-item label="密码:" prop="password">
                <el-input v-model="ruleForm.password" placeholder="请输入密码" maxlength="20"/>
            </el-form-item>
            <el-form-item label="性别:" prop="sex">
                <el-radio-group v-model="ruleForm.sex">
                    <el-radio label="男"></el-radio>
                    <el-radio label="女"></el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="家庭住址:" prop="location">
                <el-input v-model="ruleForm.location" placeholder="请输入家庭住址" maxlength="50"/>
            </el-form-item>
            <el-form-item label="电话:" prop="phone">
                <el-input v-model="ruleForm.phone" placeholder="请输入电话" maxlength="20"/>
            </el-form-item>
            <el-form-item label="工作地:" prop="workLocation">
                <el-input v-model="ruleForm.workLocation" placeholder="请输入工作地" maxlength="50"/>
            </el-form-item>
            <el-form-item label="身份证:" prop="identityCard">
                <el-input v-model="ruleForm.identityCard" placeholder="请输入身份证号码" maxlength="20"/>
            </el-form-item>
            <div class="subBox address">
                <el-form-item>
                    <el-button @click="goBack()">
                        取消
                    </el-button>
                    <el-button type="primary" @click="submitForm('ruleForm', false)">
                        保存
                    </el-button>
                    <el-button v-if="actionType == 'add'" type="primary" class="continue" @click="submitForm('ruleForm', true)">
                        保存并继续添加
                    </el-button>
                </el-form-item>
            </div>
        </el-form>
    </div>
</div>
<script src="../../plugins/vue/vue.js"></script>
<script src="../../plugins/element-ui/index.js"></script>
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../api/member.js"></script>
<script src="../../js/validate.js"></script>
<script src="../../js/index.js"></script>
<script>
    new Vue({
        el: '#member-add-app',
        data() {
            return {
                id: '',
                actionType: '',
                ruleForm: {
                    'username': '',
                    'identityCard': '',
                    'sex': '男',
                    'location': '',
                    'password': '',
                    'phone': '',
                    'workLocation': '',
                    'povertyTypes': ''
                }
            }
        },
        computed: {
        },
        created() {
            this.id = requestUrlParam('id')
            this.actionType = this.id ? 'edit' : 'add'
            if (this.id) {
                this.init()
            }
        },
        mounted() {
        },
        methods: {
            async init() {
                queryOperatorById(this.id).then(res => {
                    console.log(res)
                    if (String(res.code) === '1') {
                        console.log(res.data)
                        this.ruleForm = res.data
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                })
            },
            submitForm(formName, st) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if (this.actionType === 'add') {
                            const params = {
                                ...this.ruleForm,
                            }
                            addOperator(params).then(res => {
                                if (res.code === 1) {
                                    this.$message.success('操作员添加成功！')
                                    if (!st) {
                                        this.goBack()
                                    } else {
                                        this.ruleForm = {
                                            'username': '',
                                            'identityCard': '',
                                            'sex': '男',
                                            'location': '',
                                            'area': '',
                                            'phone': '',
                                            'password': '',
                                            'workLocation': ''
                                        }
                                    }
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })
                        } else {
                            const params = {
                                ...this.ruleForm,
                            }
                            updateOperator(params).then(res => {
                                if (res.code === 1) {
                                    this.$message.success('员工信息修改成功！')
                                    this.goBack()
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })
                        }
                    } else {
                        console.log('error submit!!')
                        return false
                    }
                })
            },
            goBack() {
                window.parent.menuHandle({
                    id: '2',
                    url: '/backend/page/operator/list2.html',
                    name: '操作员模块'
                }, false)
            }
        }
    })
</script>
</body>
</html>